<?php
/**
 * Functions taken from the formatting.php file of the Genesis Framework(tm),
 * in order to keep the compatibility of the plugin even if the Genesis Framework
 * is not active.
 */

class DPEM_Utilities {
    public static function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0) {
        $content = get_the_content_limit($max_char, $more_link_text, $stripteaser);
        echo apply_filters('the_content_limit', $content);
    }

    public static function get_the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0) {
        $content = get_the_content('', $stripteaser);
        // Strip tags and shortcodes
        $content = strip_tags(strip_shortcodes($content), apply_filters('get_the_content_limit_allowedtags', '<script>,<style>'));
        // Inline styles/scripts
        $content = trim(preg_replace('#<(s(cript|tyle)).*?</\1>#si', '', $content));
        // Truncate $content to $max_char
        $content = truncate_phrase($content, $max_char);
        // More Link?
        if ( $more_link_text ) {
                $link = apply_filters( 'get_the_content_more_link', sprintf( '%s <a href="%s" class="more-link">%s</a>', g_ent('&hellip;'), get_permalink(), $more_link_text ), $more_link_text );
                $output = sprintf('<p>%s %s</p>', $content, $link);
        }
        else {
                $output = sprintf('<p>%s</p>', $content);
        }
        return apply_filters('get_the_content_limit', $output, $content, $link, $max_char);
    }

    public static function g_ent( $text = '' ) { return apply_filters('g_ent', $text); }

    public static function truncate_phrase($phrase, $max_characters) {
        $phrase = trim( $phrase );
        if ( strlen($phrase) > $max_characters ) {
                // Truncate $phrase to $max_characters + 1
                $phrase = substr($phrase, 0, $max_characters + 1);
                // Truncate to the last space in the truncated string.
                $phrase = trim(substr($phrase, 0, strrpos($phrase, ' ')));
        }
        return $phrase;
    }

    public static function get_additional_image_sizes() {
        global $_wp_additional_image_sizes;

        if ( $_wp_additional_image_sizes )
                return $_wp_additional_image_sizes;

        return array();
    }
}

?>
